package com.yunyu.demo;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.CheckBox;
import android.widget.SeekBar;

import androidx.appcompat.app.AppCompatActivity;

import com.yunyu.camera.filter.AspectFit;
import com.yunyu.camera.filter.FaceFrameWidget;
import com.yunyu.camera.filter.FaceFrameWidgets;
import com.yunyu.camera.filter.FaceModelBuild;
import com.yunyu.camera.filter.StickerRender;
import com.yunyu.camera.opengl.LTRTCFilterManager;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

public class CameraActivityHelper {
    private AppCompatActivity appCompatActivity;

    //美白参数
    private int brightFace = 50;
    //磨皮参数
    private int smoothFace = 70;
    //大眼参数
    private int bigEye = 50;
    //瘦脸参数
    private int thinFace = 50;

    public CameraActivityHelper(AppCompatActivity appCompatActivity){
        this.appCompatActivity = appCompatActivity;
    }

    public void testControl(LTRTCFilterManager filterManager){
        //开启美白磨皮
        filterManager.setBeautyFilterParam(smoothFace,brightFace);
        //开启美妆
        filterManager.setFaceMakeupFilterParam(bigEye,thinFace);
        //开启人脸特效1
        filterManager.setFaceStickerFilterParam(createFaceModelBuild().getStickerRenderMap());
//        //开启代码特效2
//        filterManager.setCodeEffectFilterParam(LTRTCFilterManager.CodeEffectFilterType.GLITCH);
//        //开启pag特效3
//        filterManager.setPagOverBlendFilterParam(getAssets(), "testData/test.pag");
//        //开启静态相框特效
//        filterManager.setBitmapFilterParam(loadAssertImage("frame.png"));
//        //开启lookup滤镜
//        filterManager.setLookupFilterBitmap(loadAssertImage("lookup.png"));

        //人脸效果开关
        CheckBox checkBox = appCompatActivity.findViewById(R.id.glBeauty);
        checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked){
                filterManager.setFaceMakeupFilterParam(bigEye,thinFace);
                filterManager.setBeautyFilterParam(smoothFace,brightFace);
            }
            else {
                filterManager.disableFaceMakeupFilter();
                filterManager.disableBeautyFilter();
            }
        });

        //大眼控制
        SeekBar bigEyeBar = appCompatActivity.findViewById(R.id.bigEyeSeekBar);
        bigEyeBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                bigEye = progress;
                filterManager.setFaceMakeupFilterParam(bigEye,thinFace);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        //瘦脸
        SeekBar thinFaceBar = appCompatActivity.findViewById(R.id.thinFaceSeekBar);
        thinFaceBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                thinFace = progress;
                filterManager.setFaceMakeupFilterParam(bigEye,thinFace);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        //美白
        SeekBar brightFaceBar = appCompatActivity.findViewById(R.id.brightFaceSeekBar);
        brightFaceBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                brightFace = progress;
                filterManager.setBeautyFilterParam(smoothFace,brightFace);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        //磨皮
        SeekBar smoothFaceBar = appCompatActivity.findViewById(R.id.smoothFaceSeekBar);
        smoothFaceBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                smoothFace = progress;
                filterManager.setBeautyFilterParam(smoothFace,brightFace);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        //代码特效开关
        CheckBox openglEffects = appCompatActivity.findViewById(R.id.openglEffects);
        openglEffects.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked){
                filterManager.setCodeEffectFilterParam(LTRTCFilterManager.CodeEffectFilterType.SOUL_OUT);
            }
            else {
                filterManager.disableCodeEffectFilter();
            }
        });

        //人脸道具
        CheckBox facePropCB = appCompatActivity.findViewById(R.id.faceProp);
        facePropCB.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked){
                filterManager.setFaceStickerFilterParam(createFaceModelBuild().getStickerRenderMap());
            }
            else {
                filterManager.disableFaceStickerFilter();
            }
        });

        //静态相框
        CheckBox staticFrameCB = appCompatActivity.findViewById(R.id.staticFrame);
        staticFrameCB.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked){
                filterManager.setBitmapFilterParam(loadAssertImage("frame.png"));
            }
            else {
                filterManager.disableBitmapFilter();
            }
        });

        //动态相框
        CheckBox dynamicFrameCB = appCompatActivity.findViewById(R.id.dynamicFrame);
        dynamicFrameCB.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked){
                filterManager.setPagOverBlendFilterParam(appCompatActivity.getAssets(), "test.pag");
            }
            else {
                filterManager.disablePagOverBlendFilter();
            }
        });

        //滤镜
        CheckBox glFilterCB = appCompatActivity.findViewById(R.id.glFilter);
        glFilterCB.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked){
                filterManager.setLookupFilterBitmap(loadAssertImage("lookup.png"));
            }
            else {
                filterManager.disableLookupFilterBitmap();
            }
        });
    }

    //region model

    /**
     * 构建人脸贴纸模型
     * @return 模型
     */
    FaceModelBuild createFaceModelBuild(){
        //模拟加载人脸道具
        FaceModelBuild faceModelBuild = new FaceModelBuild();

        //人脸动态贴纸
        StickerRender t = faceModelBuild.createPagSticker(appCompatActivity.getAssets(),"testFaceHat.pag",75);
        //android默认只有106点位,如果需要将道具对齐到其它点位的 可以按照就近原则做适当偏移即可
        //设置帽子道具的偏移值,x:左右偏移 y:上下偏移 z:远近偏移 单位为像素
        faceModelBuild.setOffset(t,160,-250,0);
        //将StickerRender对象关联至faceModelBuild
        faceModelBuild.linkRender(t);
        //同理创建第二个人脸部位:眼镜
        StickerRender t2 = faceModelBuild.createPagSticker(appCompatActivity.getAssets(),"testGlass.pag",21);
        faceModelBuild.linkRender(t2);

        //人脸挂件
        FaceFrameWidgets faceFrameWidgets = new FaceFrameWidgets();//创建挂件集合
        List<FaceFrameWidget> widgets = new LinkedList<>();
        //创建一张贴纸
        StickerRender testPagWidget = faceModelBuild.createPagWidget(appCompatActivity.getAssets(),"testWidget.pag");//pag
        //贴纸关联为人脸挂件
        FaceFrameWidget faceFrameWidget = new FaceFrameWidget(testPagWidget);
        //设置原点坐标系:// lt:左上角 rt:右上角 lb:左下角 rb右下角 //坐标系原点  说明:x:右边x为+ 左边为-  y:上面为+ 下面为-
        faceFrameWidget.setCs("lt");
        //对齐方式
        // auto:自动对齐:此模式会按照百分比模式调节挂件坐标,例如设计尺寸高度为720 偏移y为360则挂件高度居中显示
        // static:固定模式:此模式为固定模式,设置偏移100则相对于原点坐标只会便宜100像素
        faceFrameWidget.setMode("auto");
        //设置相对原点的偏移值,单位为像素
        faceFrameWidget.setX(100);
        faceFrameWidget.setY(0);
        //设置素材的ui设计尺寸一般为720x1280
        faceFrameWidget.setDesignW(720);
        faceFrameWidget.setDesignH(1280);
        //添加入挂件集合
        widgets.add(faceFrameWidget);
        //关联至根集合
        faceFrameWidgets.setWidgets(widgets);
        faceModelBuild.linkRender(faceFrameWidgets);

        //人脸相框
        //pagName4x3和pagName1x1位可选传值,默认显示16x9比例图 如果相机尺寸比例切换后 渲染内部会自动选取对应比例资源显示 如果为null则回压缩显示
        StickerRender testPagFrame = faceModelBuild.createPagFrame(appCompatActivity.getAssets(),"testFrame.pag", null, null, AspectFit.Aspect16x9);
        faceModelBuild.linkRender(testPagFrame);

        return faceModelBuild;
    }

    /**
     * 加载资源文件
     * @param name 图片资源名称
     * @return 图片
     */
    Bitmap loadAssertImage(String name){
        Bitmap bitmap = null;
        try {
            AssetManager assetManager = appCompatActivity.getResources().getAssets();
            InputStream stream = assetManager.open(name);
            bitmap = BitmapFactory.decodeStream(stream);
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    //endregion
}
